package com.py.message.listener;


import com.py.message.config.MyWebSocket;
import com.py.message.domain.Message;
import com.py.message.service.IMessageService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Component
public class MessageListener {

    @Resource
    private IMessageService messageService;
    @Resource
    MyWebSocket myWebSocket;


    @RabbitListener(queues = "check")
    public void receive(String msg) throws IOException {
        Message message=new Message();
        message.setContent(msg);
        message.setType("0");
        message.setState("1");
        message.setTime(new Date());
        int i= messageService.insertMessage(message);
        if (i>0){
            System.out.println("receive1 check:"+msg);
            myWebSocket.sendToAllClient(msg);
        }
    }

    @RabbitListener(queues = "kucun")
    public void kucun(String msg) throws IOException {
        System.out.println("msg:"+msg);
        Message message=new Message();
        message.setContent(msg);
        message.setType("3");
        message.setState("1");
        message.setTime(new Date());
        int i= messageService.insertMessage(message);
        if (i>0){
            System.out.println("kucun:"+msg);
            myWebSocket.sendToAllClient(msg);
        }
    }


    @RabbitListener(queues = "mesShe")
    public void mesShe(String msg) throws IOException {
        System.out.println(msg);
        myWebSocket.sendToAllClient("111");
    }
}
